package com.b1502.store2.fragment;


import android.graphics.Rect;

import android.os.Bundle;
import android.support.annotation.Nullable;

import android.support.v7.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.b1502.store2.R;



import com.b1502.store2.adapter.HomeProductAdapter;

import com.b1502.store2.model.bean.HomeProductsBean;

import com.b1502.store2.model.AdvertItem;

import com.b1502.store2.network.StoreParams;

import com.b1502.store2.util.GlideImageLoader;
import com.b1502.store2.util.UrlUtil;



import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;


import com.youth.banner.Banner;


import org.mym.plog.PLog;
import org.xutils.common.Callback;
import org.xutils.x;



import java.util.ArrayList;

import java.util.List;


public class HomeFragment extends BaseFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private List<String> stringList=new ArrayList<>();
    private List<String> stringList2=new ArrayList<>();
    private String mParam1;
    private String mParam2;
    private View view;
    private XRecyclerView xRecyclerView;
    private Banner banner;

    public HomeFragment() {
        // Required empty public constructor
    }


    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_home, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        banner=  (Banner) view.findViewById(R.id.banner);
        xRecyclerView=  (XRecyclerView) view.findViewById(R.id.recycler_view);
        xRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2));
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {

            @Override
            public void onRefresh() {



            }

            @Override
            public void onLoadMore() {



            }
        });
        getAdvertItems();
        getHomeProducts(0);


    }

    //处理首页产品数据
    private void getHomeProducts(int pageIndex) {

        StoreParams params = new StoreParams(UrlUtil.GetHomeProducts, pageIndex);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onSuccess(String result) {

                HomeProductsBean[] products = new Gson().fromJson(result, HomeProductsBean[].class);
                HomeProductAdapter homeProductAdapter=new HomeProductAdapter(getActivity(),products);
                xRecyclerView.setAdapter(homeProductAdapter);
                int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.space);
                xRecyclerView.addItemDecoration(new SpaceItemDecoration(spacingInPixels));
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    //处理首页轮播图
    private void getAdvertItems() {
        StoreParams params = new StoreParams(UrlUtil.GetAdvertItems);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onSuccess(String result) {


                AdvertItem[] advertItem = new Gson().fromJson(result, AdvertItem[].class);

                AdvertItem advertItem1 = advertItem[0];
                AdvertItem advertItem2 = advertItem[1];
                AdvertItem advertItem3 = advertItem[2];

                stringList.add(advertItem1.imgUrl);
                stringList.add(advertItem2.imgUrl);
                stringList.add(advertItem3.imgUrl);
                for (int i=0;i<stringList.size();i++){


                    String imageUrl = UrlUtil.getImageUrl(stringList.get(i));
                    stringList2.add(imageUrl);


                }
                //适配轮播图
                banner.setImageLoader(new GlideImageLoader());
                banner.setImages(stringList2);
                banner.start();

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    //设置子条目的间距
    public class SpaceItemDecoration extends XRecyclerView.ItemDecoration{

        private int space;

        public SpaceItemDecoration(int space) {
            this.space = space;
        }

        public void getItemOffsets(Rect outRect, View view, XRecyclerView parent, XRecyclerView.State state) {

            if(parent.getChildPosition(view) != 0)
                outRect.top = space;
        }
    }
}
